package com.wust.loveangel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.wust.R;
import com.wust.data.Data;
import com.wust.data.DataDao;
import com.wust.data.KnowledgeMap;
import com.wust.data.MapDao;
import com.wust.menu.ThirdGroupTab;


import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class loveAngelActivity4 extends ListActivity {
	private ListView listview=null;
	private ArrayList<HashMap<String, Object>> listItems;
	private TextView loveangel_title=null;
	private Button loveangel_back=null;
	private MapDao mapDao;
	private DataDao dataDao;
	private List<Data>datalist;
	private String id4;
	private String id2;
	private String id3;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.loveangel2);
		loveangel_back=(Button)findViewById(R.id.loveangel_back);
		loveangel_back.setOnClickListener(new OnListener());
		loveangel_title=(TextView)findViewById(R.id.loveangel_title);
		mapDao=new MapDao(this);
		dataDao=new DataDao(this);
		Intent intent=getIntent();
		id4=intent.getStringExtra("msg");
		id2=intent.getStringExtra("id2");
		id3=intent.getStringExtra("id3");
		KnowledgeMap kmap=mapDao.getMapById(id4);
		loveangel_title.setText(kmap.getCat_name());	
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		listview=getListView();
		listview.setAdapter(buildAdapter());
		super.onResume();
	}
	private  SimpleAdapter buildAdapter(){
		listItems = new ArrayList<HashMap<String, Object>>();    
		datalist=dataDao.queryByCatId(id4);
		for (Iterator iterator = datalist.iterator(); iterator.hasNext();) {
			Data data = (Data) iterator.next();
			HashMap<String, Object> map = new HashMap<String, Object>();           	        
	        map.put("loveangel_two_title", data.getTitle()); 
	        map.put("loveangel_two_content", data.getContent()); 
	        listItems.add(map); 
		}       
        //生成适配器的Item和动态数组对应的元素       
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItems,//数据源        
            R.layout.loveangel_two_item,//ListItem的XML布局实现       
            //动态数组与ImageItem对应的子项               
            new String[] {"loveangel_two_title","loveangel_two_content"},        
            //ImageItem的XML文件里面的一个ImageView,两个TextView ID       
            new int[] {R.id.loveangel_two_title,R.id.loveangel_two_content}       
        ); 
        return listItemAdapter;
	}
	private void buildList(){
		listview.setAdapter(buildAdapter());
	}
	@Override  
    public void onBackPressed() {   
         back();
    } 
	private void back(){
		//把后退事件交给子Activity处理  
		Intent intent = new Intent(loveAngelActivity4.this, loveAngelActivity3.class);  
		// 把Activity转换成一个Window，然后转换成View  
		intent.putExtra("msg", id3);
		intent.putExtra("id2", id2);
		Window w = ThirdGroupTab.group.getLocalActivityManager()  
		            .startActivity("loveAngelActivity4",intent);  
		View view = w.getDecorView();  
		//设置要跳转的Activity显示为本ActivityGroup的内容  
		ThirdGroupTab.group.setContentView(view); 	
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long ids) {
		// TODO Auto-generated method stub
		
		super.onListItemClick(l, v, position, ids);
		//要跳转的Activity  
		Intent intent=new Intent();
		intent.setClass(this, loveAngelActivity3.class);
		String msg=String.valueOf(datalist.get(position).getId());	
		jumptoActivity(loveAngelActivity5.class,"loveAngelActivity5",new String[]{"msg","id4","id2","id3"},new String[]{msg,id4,id2,id3});
			
	}
	private void jumptoActivity(Class activity,String activityname ,String key[],String value[]){
		Intent intent = new Intent(loveAngelActivity4.this, activity);  
		if(key!=null&&value!=null&&key.length==value.length)	
			for(int i=0;i<key.length;++i)
				intent.putExtra(key[i], value[i]);
        // 把Activity转换成一个Window，然后转换成View  
        Window w = ThirdGroupTab.group.getLocalActivityManager()  
                .startActivity(activityname,intent);  
        View view = w.getDecorView();  
        //设置要跳转的Activity显示为本ActivityGroup的内容  
        ThirdGroupTab.group.setContentView(view); 
	}
    class OnListener implements OnClickListener{
    	@Override
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		back();
    	}
    }
}
